package com.kdtech.analyse.video;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

public class DqdailyAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://www.dqntv.com/dianshidianbo/2015-06-26/21135.html
		String[] regex = {
				"http://www.dqntv.com/dianshidianbo/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]+.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		video.setUrl(url);		
		String title = null;
		String desc = null;
		Long date = 0l;
		
			
		
		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题
		 */
		title = doc.select("div.video h1").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("title").text().trim();
			title = StringUtils.substringBefore(title, "_");
		}
		
		/*
		 * 解析时间
		 */
		String text = doc.select("td[align=center]:contains(时间)").text();
		date = DateUtils.matchDate(text);
		
		if(StringUtils.isBlank(title)){
		}
		video.setTitle(title);
		video.setDate(date);
		return video;
	}
	
	public NewsMeta Update(NewsMeta meta) {
	
		return null;
	}
	
	public static void main(String[] args) {
		DqdailyAnalyse a = new DqdailyAnalyse();
		String url = "http://www.dqntv.com/dianshidianbo/2015-06-25/21000.html";
		if(a.isDetailPage(url)){
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}else{
			System.out.println("不匹配规则");
		}
		
	}

	

	
}
